home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Mania 6
/
MacMania 6.toast
/
/
Tools&Utilities
/
EnterAct Stuff
/
Drag_on Modules
/
hAWK programs
/
$TabsToSpaces
< prev
next >
Wrap
Text File
|
1993-04-09
|
4KB
|
124 lines
#$TabsToSpaces: converts tabs to spaces in one or more documents,
#replacing each tab by the appropriate number of spaces (anywhere
#from 1 to “spaces_in_tabs”), consistent with the tab interpretation
#of THINK C et al.
#Use “Set variables” in the setup dialog to set the following:
#Variable=default meaning
#---------------- ----------------
# spaces_in_tab=4 the number of spaces per tab in your documents
# overwrite=0 uses the function “MakeNewFileName()” to
# produce a name for the copy of the file
# (if =1, overwrites the
# original file).
#
#Run on a specific file or on MFS selected files. Overwrites original
#file if overwrite=1, so MAKE A COPY FIRST if you want to keep original.
#Note if you would prefer to make a copy of the file rather than
#overwriting the original, leave “overwrite=0”; this invokes the
#function MakeNewFileName() which appends “s” (for space) to the
#file name, eg turning "main.c" into "main.cs". You can change this
#function to produce different names for the copies, even
#autoincrementing a version number with something like:
# -- split FILENAME into names[] as in MakeNewFileName()
# # assuming names look like "ProperNameNNN.c" where NNN = version number
# if (match(names[z], /[0-9]+\./)) #digits followed by period
# {
# num = substr(names[z], RSTART, RLENGTH-1) #skip period
# ++num #hAWK string/number power at work
# sub(/[0-9]+\./, num ".", names[z]) #remember to put back the matched period
# }
# else if (match(names[z], /\./)) #no version number yet
# sub(/\./. "1.", names[z])
# else #give up, tack on an s.
# names[z] = names[z] "s"
# -- the rest as in MakeNewFileName()
# User’s Manual references:
# «hAWK User’s Manual» «F Running hAWK programs»
# «hAWK User’s Manual» «L 5 Regular expressions»
# «hAWK User’s Manual» «M 5 Built-in string and file functions»
# «hAWK User’s Manual» «K 4 Built-in variables»
# «hAWK User’s Manual» «K 8 Arrays»
# «hAWK User’s Manual» «N User-defined functions»
# «hAWK User’s Manual» «P 3 The getline function»
# «hAWK User’s Manual» «O 3 Output into files»
# «hAWK User’s Manual» «Q The hAWK function»
BEGIN {
#For position of tab from 1 to 200 or so, precompute number of
#spaces required to replace tab at that position.
for (i = 0; i <= 200; ++i)
n_first_tab[i+1] = (int((i)/spaces_in_tab)+1)*spaces_in_tab - i;
#For speed, set up array of spaces.
spaces[1] = " "
for (i = 2; i <= 100; ++i)
spaces[i] = spaces[i-1] " ";
n = 0#not necessary, but helps reading
if (overwrite+0 != 0)
overwrite = 1
else
overwrite = 0
}
FNR == 1{ #at the first line of a file...
#Flush buffer if overwriting file.
if (n > 0)
{
for (i = 1; i <= n; ++i)
{
print out[i] > outfile;
delete out[i];
}
close(outfile);
n = 0;
}
else if (outfile != "")
close(outfile);
#Standard action is to overwrite the input file with the altered version.
if (overwrite)
outfile = FILENAME;
else
outfile = MakeNewFileName()
}
#The main event: rep tabs with spaces, and print immediately if copying file.
#Buffer altered lines to the out[] array if overwriting.
#Note if hAWK gives up due to out-of-memory while buffering up, then
#the orginal file will not be touched. The “delete out[i]” above
#frees up memory for each new file.
{ # No action implies this pattern executed for all input lines...
while (match($0, /\t+/)) # sets RSTART, RLENGTH
{
#First tab in group counts for a variable number of spaces.
total_sp = n_first_tab[RSTART] + spaces_in_tab *(RLENGTH-1)
sub(/\t+/, spaces[total_sp]) #replaces leftmost group only
}
if (overwrite)
out[++n] = $0
else
print $0 > outfile
}
END {
if (n > 0)
{
for (i = 1; i <= n; ++i)
{
print out[i] > outfile;
}
close(outfile);
}
}
function MakeNewFileName( z, i, outfile)
{
z = split(FILENAME, names, ":");
names[z] = names[z] "s"
if (length(names[z]) > 31)
names[z] = substr(names[z], 2)#trim first letter - not elegant...
outfile = names[z]
for (i = z-1; i >= 1; --i) #put full path name back together
outfile = names[i] ":" outfile;
return outfile
}